package com.laikan.reader.core.view;

/**
 * @author Kiritor
 * 实现自己的View继承Checable接口
 */

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.laikan.reader.R;

public class GridItem extends RelativeLayout implements Checkable {

    private Context mContext;
    private boolean mChecked;//判断该选项是否被选上的标志量
    private ImageView mImgView = null;
    private ImageView mSecletView = null;
    private RelativeLayout rl_imgs;

    public GridItem(Context context) {
        this(context, null, 0);
        this.setSelected(false);
    }

    public GridItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GridItem(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        LayoutInflater.from(mContext).inflate(R.layout.lk_mybook_grid_item, this);
        mImgView = (ImageView)findViewById(R.id.img_view);
        mSecletView = (ImageView)findViewById(R.id.select);
        rl_imgs = (RelativeLayout)findViewById(R.id.rl_imgs);
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void setChecked(boolean checked) {
        if (!rl_imgs.isShown()) {
            mChecked = checked;
            mSecletView.setVisibility(checked ? View.VISIBLE : View.GONE);//选上了则显示小勾图片
        }
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }

    public void setImgResId(int resId) {
        if (mImgView != null) {
            mImgView.setBackgroundResource(resId);//设置背景
        }
    }

}
